#! /usr/bin/env python

import pygame
import math

WIDTH = 640
HEIGHT = 480
RED = (255, 0, 0)
GREEN = (0, 255, 0)
BLUE = (0, 0, 255)
GREY = (128, 128, 128)
DGREY = (20, 20, 20)
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)

RADIUS = 128

x = y = 0
correctedx = correctedy = 0
magnitude = 0
direction = 0

font = 0
text = 0
string0 = 0
string1 = 0
string2 = 0
string3 = 0
string4 = 0

screen = pygame.display.set_mode((WIDTH, HEIGHT))
center = (WIDTH/2, HEIGHT/2)
running = 1

eyeLine = 0
eyex = 0
eyey = 0 
pupilLoc = 0
maxMag = math.sqrt(center[0] * center[0] + center[1] * center[1])


pygame.font.init()

while running:
	event = pygame.event.poll()
	if event.type == pygame.QUIT:
		running = 0
	elif event.type == pygame.MOUSEMOTION:
		x, y = event.pos
		
		correctedx = x - center[0]
		correctedy = center[1] - y
		magnitude = math.sqrt(correctedx * correctedx + correctedy * correctedy)
		if correctedx != 0:
			direction = math.degrees(math.atan((correctedy + 0.0)/(correctedx + 0.0)))
		string0 = "(%s, %s)" % (correctedx, correctedy)
		string1 = "mag = %s" % magnitude
		if (x < center[0]) and (y < center[1]):
			direction += 180
		elif (x < center[0]) and (y > center[1]):
			direction += 180
		elif (x > center[0]) and (y > center[1]):
			direction += 360
		elif (x > center[0]) and (y == center[1]):
			direction = 0.0
		elif (x < center[0]) and (y == center[1]):
			direction = 180.0
		elif (x == center[0]) and (y > center[1]):
			direction = 270.0
		elif (x == center[0]) and (y < center[1]):
			direction = 90.0
	
	screen.fill(WHITE)
	pygame.draw.circle(screen, BLACK, (x,y), 4, 2)
	pygame.draw.circle(screen, GREY, center, RADIUS, 1)	
	if pygame.font:
		font = pygame.font.Font(None, 20)
		text = font.render(string0, 1, DGREY)
		screen.blit(text, (0,0))
		text = font.render(string1, 1, DGREY)
		screen.blit(text, (0,20))
		string2 = "dir = %s" % direction
		text = font.render(string2, 1, DGREY)
		screen.blit(text, (0,40))

	eyeLine = (RADIUS-12) * ((magnitude + 0.0) / (maxMag + 0.0))
	eyex = int(eyeLine * math.cos(math.radians(direction))) + center[0]
	eyey = -1 * int(eyeLine * math.sin(math.radians(direction))) + center[1]

	#pygame.draw.aaline(screen, BLACK, center, (x,y))
	if (x > center[0]) and (y < center[1]):
		# pygame.draw.aaline(screen, RED, center, (x, HEIGHT/2))
		# pygame.draw.aaline(screen, RED, (x, HEIGHT/2), (x,y))
		pygame.draw.circle(screen, RED, (eyex, eyey), 12, 0)
	elif (x < center[0]) and (y < center[1]):
		# pygame.draw.aaline(screen, GREEN, center, (x, HEIGHT/2))
		# pygame.draw.aaline(screen, GREEN, (x, HEIGHT/2), (x,y))
		pygame.draw.circle(screen, GREEN, (eyex, eyey), 12, 0)
	elif (x < center[0]) and (y > center[1]):
		# pygame.draw.aaline(screen, BLUE, center, (x, HEIGHT/2))
		# pygame.draw.aaline(screen, BLUE, (x, HEIGHT/2), (x,y))
		pygame.draw.circle(screen, BLUE, (eyex, eyey), 12, 0)
	elif (x > center[0]) and (y > center[1]):
		# pygame.draw.aaline(screen, GREY, center, (x, HEIGHT/2))
		# pygame.draw.aaline(screen, GREY, (x, HEIGHT/2), (x,y))
		pygame.draw.circle(screen, BLACK, (eyex, eyey), 12, 0)
	
	

	# string3 = "maxMag = %s" % maxMag
	# text = font.render(string3, 1, DGREY)
	# screen.blit(text, (0,60))
	string4 = "(%s, %s)  eyeMag = %s" % (eyex, eyey, eyeLine)
	text = font.render(string4, 1, DGREY)
	screen.blit(text, (0,80))
	
	pygame.display.flip()
